package com.situ.mall.controller;

import com.situ.common.util.PageResult;
import com.situ.common.util.Result;
import com.situ.mall.pojo.entity.*;
import com.situ.mall.pojo.query.OrderQuery;
import com.situ.mall.pojo.vo.OrderVO;
import com.situ.mall.service.ICouponService;
import com.situ.mall.service.IDrawService;
import com.situ.mall.service.IOrderService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IDrawService drawService;
    @Autowired
    private ICouponService couponService;

    @ResponseBody
    @RequestMapping("/add")
    public Result add(Order order, HttpSession session) {
        User user = (User) session.getAttribute("user");
        order.setUserId(user.getId());
        Draw draw = drawService.selectByUserId(user.getId());
        if (!ObjectUtils.isEmpty(order.getShippingId())) {
            if (!ObjectUtils.isEmpty(draw)) {
                drawService.addNumber(user.getId());
            } else {
                Draw newDraw = new Draw();
                newDraw.setUserId(user.getId());
                newDraw.setDrawNumber(1);
                drawService.addDraw(newDraw);
            }
            orderService.add(order);
            Coupon coupon = new Coupon();
            coupon.setId(order.getCouponId());
            coupon.setIsDeleted((byte) 1);
            couponService.updateCoupon(coupon);
            return Result.ok("下单成功！");
        } else {
            return Result.error("请先添加收货地址！");
        }

    }
    @RequestMapping("/selectByPage")
    @ResponseBody
    public PageResult selectByPage(OrderQuery orderQuery, HttpSession session) {
        User user = (User) session.getAttribute("user");
        orderQuery.setUserId(user.getId());
        PageResult pageInfo = orderService.selectByPage(orderQuery);
        System.out.println(orderQuery);
        return pageInfo;
    }
    @RequestMapping("/orderItemByOrderNo")
    @ResponseBody
    public Result orderItemByOrderNo(Long orderNo) {
        List<OrderItem> list = orderService.orderItemByOrderNo(orderNo);
        return Result.ok(list);
    }
    @RequestMapping("/list")
    @ResponseBody
    public Result list(OrderQuery orderQuery, HttpSession session) {
        User user = (User) session.getAttribute("user");
        orderQuery.setUserId(user.getId());
        List<OrderVO> list = orderService.list(orderQuery);
        return Result.ok(list);
    }
    @ResponseBody
    @RequestMapping("/cancelOrder")
    public Result cancelOrder(Order order) {
        order.setStatus(0);
        orderService.cancelOrder(order);
        Order cancelOrder = orderService.orderByOrderNo(order.getOrderNo());
        Coupon coupon = new Coupon();
        coupon.setId(cancelOrder.getCouponId());
        coupon.setIsDeleted((byte) 0);
        couponService.updateCoupon(coupon);
        return Result.ok("取消成功！");
    }
    @RequestMapping("/orderByOrderNo")
    @ResponseBody
    public Result orderByOrderNo(Long orderNo) {
        Order order = orderService.orderByOrderNo(orderNo);
        return Result.ok(order);
    }
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public Result deleteOrder(Order order) {
        Order ordersInfo = orderService.orderByOrderNo(order.getOrderNo());
        if (ordersInfo.getStatus() == 0 || ordersInfo.getStatus() == 60) {
            order.setIsDeleted((byte) 1);
            orderService.deleteOrder(order);
            return Result.ok("删除成功！");
        } else {
            return Result.error("订单尚未结束");
        }

    }
    @RequestMapping("/update")
    @ResponseBody
    public Result update(Order order) {
         orderService.update(order);
        return Result.ok("修改成功！");
    }
    @RequestMapping("/payOrderSuccess")
    @ResponseBody
    public Result payOrderSuccess(Order order) {
        order.setStatus(20);
        Date date = new Date();
        order.setPaymentTime(date);
        orderService.payOrderSuccess(order);
        return Result.ok("支付成功！");
    }
    @RequestMapping("/confirmReceipt")
    @ResponseBody
    public Result confirmReceipt(Order order) {
        order.setStatus(50);
        Date date = new Date();
        order.setEndTime(date);
        orderService.update(order);
        return Result.ok("收货成功！");
    }
    @RequestMapping("/checkUserOrder")
    @ResponseBody
    public Result checkUserOrder(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Integer orderTotal = orderService.checkUserOrder(user.getId());
        return Result.ok(orderTotal);
    }
    @RequestMapping("/buyNow")
    public Result buyNow(Order order, OrderItem orderItem, HttpSession session) {
        User user = (User) session.getAttribute("user");
        order.setUserId(user.getId());
        Draw draw = drawService.selectByUserId(user.getId());
        if (!ObjectUtils.isEmpty(order.getShippingId())) {
            if (!ObjectUtils.isEmpty(draw)) {
                drawService.addNumber(user.getId());
            } else {
                Draw newDraw = new Draw();
                newDraw.setUserId(user.getId());
                newDraw.setDrawNumber(1);
                drawService.addDraw(newDraw);
            }
            orderService.buyNow(order, orderItem);
            Coupon coupon = new Coupon();
            coupon.setId(order.getCouponId());
            coupon.setIsDeleted((byte) 1);
            couponService.updateCoupon(coupon);
            System.out.println(order);
            System.out.println(orderItem);
            return Result.ok("下单成功！");
        } else {
            return Result.error("请先添加收货地址！");
        }

    }
}
